package org.gmr;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * 日新精密删除异常数据
 */

public class RXJMDataRemove {


    public static final String authorization = "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJudW1iZXIiOiI2ODAwYzU2ZTQ2ZTBmYjAwMDc3NTMyMGYiLCJlbmhhbmNlT3JnYW5pemF0aW9uSW5mbyI6eyJyb290T3JnYW5pemF0aW9uSWQiOiJyb290X29yZ2FuaXphdGlvbl9yb290X3RlbmFudF9pZCIsInJvb3RPcmdhbml6YXRpb25OYW1lIjoi5Zu95py65LqS6IGUSURBQVMiLCJwYXJlbnRPcmdhbml6YXRpb25JZCI6IjY4MDA3MmUwNDZlMGZiMDAwNzc1MzEyZiIsImV4dGVybmFsUGFyZW50T3JnYW5pemF0aW9uSWQiOiI2ODAwNzJlMDQ2ZTBmYjAwMDc3NTMxMzAiLCJwYXJlbnRPcmdhbml6YXRpb25OYW1lIjoi6YOR5bee5pel5paw57K-5a-G5py65qKw5pyJ6ZmQ5YWs5Y-4IiwicGFyZW50T3JnYW5pemF0aW9uRnVsbFBhdGgiOiJyb290X29yZ2FuaXphdGlvbl9yb290X3RlbmFudF9pZC82MzdjNzllODQ2ZTBmYjAwMDdlZjcyMzAvNjgwMDcyZTA0NmUwZmIwMDA3NzUzMTJmIiwiZGVwYXJ0bWVudElkIjpudWxsLCJleHRlcm5hbERlcGFydG1lbnRJZCI6bnVsbCwiZGVwYXJ0bWVudE5hbWUiOm51bGwsInJvb3REZXBhcnRtZW50SWQiOm51bGwsInJvb3RFeHRlcm5hbERlcGFydG1lbnRJZCI6bnVsbCwicm9vdERlcGFydG1lbnROYW1lIjpudWxsfSwidXNlcl9uYW1lIjoicml4aW5qaW5nbWkiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwibmFtZSI6IuaXpeaWsOeyvuW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VNRU5FUkdZX1RZUEUiLCJFTlRFUlBSSVNFX01PRFVMRV9JVEVNSVpFRF9NRVRFUiIsIk9QRVJBVElPTl9NT0RVTEVfTUVURVJfVFlQRSJdLCJqdGkiOiJmYTEzZGNiNi05MjZiLTRiMmEtODU4Yy0wNzhkMWE0MGEyNTIiLCJjbGllbnRfaWQiOiIxMTY4Y2I0YS0wMmU5LTQxMTktODdlYi05N2FjZDcxOWRlYTkifQ.P71tYHCxBuuuY8zrqVaqX0bM4TDuB1H0eaJMicUD-uf7b2b2uP0tSdtikyIRTAnz0wCPGEdW7QZFaqiRASQTaGtfsPTQF6eEQIVEoiIA8Uek0f8lVbFygusQYno4S-FIHN2lSo6Iqwr5AejFsHclS701PwxCOkGw0VXSVuT_yDs7IfT6WgY7-mT5PHHu_iu_UCFDOKc1510-2q_vJ-8V3AnteOPZapiWjZY9EkmcIJVBHOTEEdSwHtTtPao8_VrOGfkHTJMBOpcCH4xaiiyEh-2FinEmLIwuZRUAxs-3PuLA_X3C0ZD9ZU406IMTIp7g9KGlmGlye5PJSPzx_izxCw";

    public static final String EleListUrl = "http://171.8.68.158:19004/ccp-ecmp-service/web-api/meter/v1/config?pageNo=1&pageSize=100&meterName=&meterDataSourceType=&energyCategory=67fdeb6de21b840001a523bb";
    public static String DataListUrl = "http://171.8.68.158:19004/ccp-ecmp-meter-data-service/web-api/meter-sequence-data/v1?meterUuid=%s&pageSize=500&pageNum=1&";
    public static final String deleteDataUrl = "http://171.8.68.158:19004/ccp-ecmp-meter-data-service/web-api/meter-sequence-data/v1?meterUuid=%s&ts=%s";


    public static final Double deleteMinValue = 1000.0;

    public static void main(String[] args) {
        addTime();
        runRemove();
    }

    public static void addTime() {
        String date = "2025-10-23";
        String time = "startTime=%s+00:00:00&endTime=%s+23:59:59";
        DataListUrl += String.format(time, date, date);
    }

    public static void runRemove() {
        // 获取表记列表
        HttpResponse response = HttpRequest.get(EleListUrl)
                .header("authorization", authorization)
                .execute();

        if (!response.isOk()) {
            System.out.println("request error");
        }

        String body = response.body();
        JSONArray records = (JSONArray) new JSONObject(body).getByPath("data.records", ArrayList.class).get(0);

        for (Object record : records) {
            JSONObject o = (JSONObject) record;

            String meterDataListUrl = String.format(DataListUrl, o.get("uuid"));
            // 获取表记对应的实时数据列表
            HttpResponse meterResponse = HttpRequest.get(meterDataListUrl)
                    .header("authorization", authorization)
                    .execute();

            if (!meterResponse.isOk()) {
                System.out.println("meterResponse error");
            }

            String meterBody = meterResponse.body();
            JSONArray data = (JSONArray) new JSONObject(meterBody).get("data");
            JSONObject object = (JSONObject) data.get(0);

            JSONArray object1 = (JSONArray) object.get("datalist");
            for (Object obj : object1) {
                JSONObject jsonObj = (JSONObject) obj;
                BigDecimal accrueValue = (BigDecimal) jsonObj.get("accrueValue");
                if (accrueValue.doubleValue() > deleteMinValue) {
                    System.out.println(jsonObj);
                    Long ts = (Long) jsonObj.get("ts");
                    String deleteDataUrlTmp = String.format(deleteDataUrl, o.get("uuid"), ts);
                    // 删除实时数据
                    HttpRequest.delete(deleteDataUrlTmp)
                            .header("authorization", authorization)
                            .execute();

                }

            }

        }


    }


}
